10.1.客户端权限验证功能完成
(1)cms/cms_profile
显示当前用户的角色和权限
角色: {% for role in user.roles %} { { role.name }} {% if not loop.last %},{% endif %} {% endfor %} 权限: {% for role in user.roles %} { { role.desc }} {% if not loop.last %}/{% endif %} {% endfor %}
(2)cms/hooks.py
@bp.context_processordef cms_context_pocessor(): return { 'CMSPermission':CMSPermission}
(3)cms/cms_base.html
不同的角色,显示不同的内容
{% set cms_user = g.cms_user %} {% if cms_user.has_permission(CMSPermission.POSTER) %}
(4)添加用户和角色
#添加访问者python manage.py create_cms_user -u 我是访问者 -p 123456 -e 11111@qq.compython manage.py add_user_to_role -e 11111@qq.com -n 访问者#添加运营者python manage.py create_cms_user -u 我是运营者 -p 123456 -e 22222@qq.compython manage.py add_user_to_role -e 22222@qq.com -n 运营#添加管理员python manage.py create_cms_user -u 我是管理员 -p 123456 -e 33333@qq.compython manage.py add_user_to_role -e 33333@qq.com -n 管理员
不同的用户登录到后台cms显示不同的内容
开发者
访问者
10.2.服务端权限验证功能完成
(1)后台剩余的html页面
cms_posts.html
{% extends 'cms/cms_base.html' %}{% block title %} 帖子管理{% endblock %}{% block head %} {% endblock %}{% block page_title %} { { self.title() }}{% endblock %}{% block main_content %} 帖子管理页面{% endblock %}
其它几个一样
cms_comments.htmlcms_boards.htmlcms_fusers.htmlcms_cusers.htmlcms_croles.html
(2)cms/decorators.py
添加一个权限验证的装饰器
def permission_required(permission): def outter(func): @wraps(func) def inner(*args,**kwargs): user = g.cms_user if user.has_permission(permission): return func(*args,**kwargs) else: return redirect(url_for('cms.index')) return inner return outter
(3)cms/views.py
@bp.route('/posts/')@login_required@permission_required(CMSPermission.POSTER)def posts(): return render_template('cms/cms_posts.html')@bp.route('/comments/')@login_required@permission_required(CMSPermission.COMMENTER)def comments(): return render_template('cms/cms_comments.html')@bp.route('/boards/')@login_required@permission_required(CMSPermission.BOARDER)def boards(): return render_template('cms/cms_boards.html')@bp.route('/fusers/')@login_required@permission_required(CMSPermission.FRONTUSER)def fusers(): return render_template('cms/cms_fusers.html')@bp.route('/cusers/')@login_required@permission_required(CMSPermission.CMSUSER)def cusers(): return render_template('cms/cms_cusers.html')@bp.route('/croles/')@login_required@permission_required(CMSPermission.ALL_PERMISSION)def croles(): return render_template('cms/cms_croles.html')
(4)cms/cms_base.html
修改url链接
{% set cms_user = g.cms_user %} {% if cms_user.has_permission(CMSPermission.POSTER) %}